import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
      // 确保这些路径与你的项目结构匹配
      '@/components': path.resolve(__dirname, './src/components'),
      '@/stores': path.resolve(__dirname, './src/stores'),
      '@/utils': path.resolve(__dirname, './src/utils')
    }
  },
  server: {
    proxy: {
      // 代理API请求到后端
      '/api': {
        target: 'http://localhost:8000', // Django后端地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    rollupOptions: {
      // 确保正确处理.vue文件
      output: {
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return id.toString().split('node_modules/')[1].split('/')[0];
          }
        }
      }
    }
  },
  // 解决TypeScript导入问题
  optimizeDeps: {
    include: [
      'vue',
      'pinia',
      'axios',
      'tdesign-vue-next'
    ]
  }
});